package cn.wolfcode.memeber.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.memeber.member.service.mapper.UserInfoMapper;
import cn.wolfcoe.luowowo.common.exception.LogicException;
import cn.wolfcoe.luowowo.common.util.Consts;
import cn.wolfcoe.luowowo.common.util.UserInfoCheck;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUserRedisService userRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey( id );
    }

    @Override
    public Boolean checkPhone(String phone) {
        int countByPhone = userInfoMapper.selectCountByPhone( phone );
        return countByPhone > 0;
    }

    @Override
    public boolean sendVerifyCode(String phone) throws LogicException {
        String verify_code_vali = " verify_code_vali:" + phone;
        if (userRedisService.getUserInfoRedis( verify_code_vali ) != null) {
            throw new LogicException( "操作频繁,请1分钟以后再试!" );
        }
        //1.创建验证码
        String code = UUID.randomUUID().toString().replaceAll( "-", "" ).substring( 0, 4 );
        String key = RedisKeys.VERIFY_CODE.join( phone );
        userRedisService.setReids( key, code, RedisKeys.VERIFY_CODE.getTime() );
        userRedisService.setverifyCodevali( verify_code_vali, phone );
        System.out.println( "您的验证码为:" + code + ",请在" + Consts.VERIFY_CODE_VAI_TIME + "内使用" );
        return true;
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        //1.验证参数是合法,自己创建一个工具类
        UserInfoCheck.hasLength( phone, "手机号不能为空" );
        UserInfoCheck.hasLength( nickname, "昵称不能为空" );
        UserInfoCheck.hasLength( password, "密码不能为空" );
        UserInfoCheck.hasLength( rpassword, "密码不能为空" );
        UserInfoCheck.hasLength( verifyCode, "验证码不能为空" );
        //2.验证两次密码是否一致
        if (password == null || rpassword == null || !password.equals( rpassword )) {
            throw new LogicException( "两次密码不一致" );
        }
        //3.验证手机号是否合法
        if (phone.length() != 11) {

            throw new LogicException( "手机号格式不正确" );
        }
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        Pattern p = Pattern.compile( regex );
        Matcher m = p.matcher( phone );
        boolean isMatch = m.matches();
        if (!isMatch) {
            throw new LogicException( "手机号格式不正确" );
        }
        //4.验证手机号是否已存在
        Boolean aBoolean = this.checkPhone( phone );
        if (aBoolean) {
            throw new LogicException( "该手机号已经注册" );
        }
        //5.验证验证码是否存在
        String key = RedisKeys.VERIFY_CODE.join( phone );
        String redisCode = userRedisService.getUserInfoRedis( key );
        //注意返回是boolean类型的数据,是可以直接取反的
        if (redisCode == null || !redisCode.equals( verifyCode )) {
            throw new LogicException( "验证码错误或者已过期" );
        }
        //6.注册用户
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname( nickname );
        userInfo.setState( userInfo.STATE_NORMAL );
        userInfo.setPassword( password );
        userInfo.setPhone( phone );
        userInfo.setHeadImgUrl( "/images/default.jpg" );
        userInfo.setLevel( 1 );
        userInfoMapper.insert( userInfo );
    }

    @Override
    public String userLogin(String username, String password) {
        UserInfo user = userInfoMapper.selectByUsernameAndPassword( username, password );
        if (user == null) {
            throw new LogicException( "用户名或密码错误" );
        }
        //用户存在,创建一个token,将token存到redis中
        String token = UUID.randomUUID().toString().replaceAll( "-", "" );
        userRedisService.addToken( token, user );
        return token;
    }

    @Override
    public List<UserInfo> list() {
        List <UserInfo> userInfos = userInfoMapper.selectAll();
        return userInfos;


    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public UserInfo queryByUserId(Long uid) {
        return userInfoMapper.selectByPrimaryKey(uid);}

    public List <Long> selectTravelsIds(Long id) {

     return    userInfoMapper.selectTravelsIds(id);
    }
    @Override
    public List <Long> selectStrategysIds(Long id) {
        return    userInfoMapper.selectStrategysIds(id);

    }


    @Override
    public List<UserInfo> queryByAuthorId(Long uid) {
        return userInfoMapper.queryByAuthorId(uid);
    }

    @Override
    public void updateHeadImgUrl(Long uid, String headImgUrl) {
        userInfoMapper.updateHeadImgUrl(uid,headImgUrl);
    }
}

